package cn.qfei.qiyuesuo.controller;

import cn.qfei.qiyuesuo.bo.FeishuSpBo;
import cn.qfei.qiyuesuo.common.EventsConstant;
import cn.qfei.qiyuesuo.common.enums.ErrorCode;
import cn.qfei.qiyuesuo.bo.FeishuNoticeBo;
import cn.qfei.qiyuesuo.service.FeishuNoticeService;
import cn.qfei.qiyuesuo.service.KaixuanSignService;
import cn.qfei.qiyuesuo.service.impl.FeishuNoticeServiceImpl;
import cn.qfei.qiyuesuo.vo.ExternalDataVo;
import cn.qfei.qiyuesuo.vo.qiyuesuo.SingOptionRes;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 开开下签章
 */
@RestController
@RequestMapping("/feishu")
@Slf4j
public class FeishuNoticeController {
    @Autowired
    private FeishuNoticeService feishuNoticeService;
    /**
     * 选择盖章id 时调取
     *
     * @throws Throwable
     */
    @PostMapping("/get/option")
    public SingOptionRes getDate(@RequestBody ExternalDataVo vo)  {
        SingOptionRes feishuNoticeDTO=new SingOptionRes();
        try {
        log.info("飞书调用获取外部数据元={}",vo);
         feishuNoticeDTO=feishuNoticeService.getSingOption(vo);
        log.info("飞书调用获取外部数据元返回={}",feishuNoticeDTO);

        }catch (Exception e){
            log.error("择盖章通知出错",e);
        }
        return feishuNoticeDTO;
    }
    /**
     * 飞书时间通知接口
     * 合同状态
     * @param request
     * @param response
     * @throws Throwable
     */
    @RequestMapping("/event")
    public void event(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        try {
        FeishuNoticeBo feishuNoticeDTO=feishuNoticeService.getFeishuNoticeDTO(request);
        if (feishuNoticeDTO==null){
            log.info("fsEvent数据为空直接返回");
            response.getWriter().write(JSON.toJSONString("SUCCESS"));
         return;
        }
        String eventType= feishuNoticeDTO.getEventType();
        if (EventsConstant.CONTRACT_CHANGE_EVENT.equals(eventType)) {//合同状态变更时，触发该事件。
            ErrorCode errorCode  =feishuNoticeService.updateFeishuNotice(feishuNoticeDTO);
            if (errorCode!=ErrorCode.SUCCESS){
                log.info("fsEvent数据处理失败返回{}",errorCode);
                response.getWriter().write(JSON.toJSONString(errorCode.getCode()));
            }
        }
        }catch (Exception e){
            log.error("合同通知出错",e);
        }
        response.getWriter().write(JSON.toJSONString("SUCCESS"));

    }

    /**
     * 飞书审批
     * @param request
     * @param response
     * @throws Throwable
     */
    @RequestMapping("sp/event")
    public void eventSp(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        try {
            log.info("飞书审批开始");
            FeishuSpBo feishuNoticeDTO=feishuNoticeService.getFeishuNoticeSpDTO(request);
            if (feishuNoticeDTO==null){
                log.info("fsEvent数据为空直接返回");
                response.getWriter().write(JSON.toJSONString("SUCCESS"));
                return;
            }
            log.info("飞书审批开始{}",feishuNoticeDTO);
            ErrorCode errorCode  =feishuNoticeService.updateSpPlace(feishuNoticeDTO);
            if (errorCode!=ErrorCode.SUCCESS){
                log.info("fsEvent数据处理失败返回{}",errorCode);
                response.getWriter().write(JSON.toJSONString(errorCode.getCode()));
            }
            log.info("飞书审批处理结束");
        }catch (Exception e){
            log.error("审批通知出错",e);
        }

        response.getWriter().write(JSON.toJSONString("SUCCESS"));

    }
}
